﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.Mvc;
using Model;
using Repository;
using UI.Helper;

namespace UI.Areas.Admin.Controllers
{
    public class FurnitureCategoryController : MyController
    {
        #region action

        public ActionResult Index()
        {
            var repository = RepositoryManager.CreateTKNTCategoryRepository();

            var lst = Paging(1, repository) ?? new List<tkntCategory>();

            return View(lst);
        }

        public ActionResult PagingData(int pageIndex)
        {
            //var repository = CreateRepository();
            var repository = RepositoryManager.CreateTKNTCategoryRepository();

            var lst = Paging(pageIndex, repository) ?? new List<tkntCategory>();

            return View("Index", lst);
        }

        public ActionResult Delete(int id)
        {
            var repository = RepositoryManager.CreateTKNTCategoryRepository();

            var objDelete = repository.HaveProduct(id);

            if (objDelete == null)
            {
                TempData[Constants.MESSAGE] = "Không tìm thấy danh mục cần xóa.";
            }

            else
            {
                if (objDelete.Products != null && objDelete.Products.Count > 0)
                    TempData[Constants.MESSAGE] = "Đang có sản phẩm thuộc danh mục '" + objDelete.ViName + "' nên không thể xóa.";
                else
                {
                    if (!string.IsNullOrEmpty(objDelete.Image))
                        System.IO.File.Delete(Path.Combine(HttpContext.Server.MapPath("~/Uploads"), objDelete.Image));
                    repository.Delete(objDelete);
                    TempData[Constants.MESSAGE] = "Đã xóa thành công";
                }
            }

            return RedirectToAction("Index");
        }

        public ActionResult DeleteAll(string id)
        {
            string[] arrId = id.Split(',');

            var i = 0;

            List<tkntCategory> lst = new List<tkntCategory>();

            var repository = RepositoryManager.CreateTKNTCategoryRepository();

            foreach (var s in arrId)
            {
                var objDelete = repository.HaveProduct(Convert.ToInt32(s));

                if (objDelete == null)
                {
                    TempData[Constants.MESSAGE] = "Không tìm thấy danh mục cần xóa.";

                    break;
                }
                else
                {
                    if (objDelete.Products != null && objDelete.Products.Count > 0)
                    {
                        TempData[Constants.MESSAGE] = "Đang có sản phẩm thuộc danh mục '" + objDelete.ViName +
                                                            "' nên không thể xóa.";

                        break;
                    }
                    else
                        lst.Add(objDelete);
                }

                i++;
            }

            if (i == arrId.Length)
            {
                foreach (var category in lst)
                {
                    if (!string.IsNullOrEmpty(category.Image))
                        System.IO.File.Delete(Path.Combine(HttpContext.Server.MapPath("~/Uploads"), category.Image));
                    repository.Delete(category);
                }

                TempData[Constants.MESSAGE] = "Đã xóa thành công";
            }

            return RedirectToAction("Index");
        }

        public ActionResult Edit(int id)
        {
            var repository = RepositoryManager.CreateTKNTCategoryRepository();

            var objEdit = repository.GetById(id);

            if (objEdit != null)
                return View(objEdit);

            return View("Index");
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(HttpPostedFileBase uploadFile)
        {
            var category = new tkntCategory();

            TryUpdateModel(category);

            var repository = RepositoryManager.CreateTKNTCategoryRepository();

            var objEdit = repository.GetById(category.Id);

            string fileName = "";
            if (uploadFile != null && uploadFile.ContentLength > 0)
            {
                fileName = Path.GetFileName(uploadFile.FileName);
                string filePath = Path.Combine(HttpContext.Server.MapPath("~/Uploads"), fileName);
                if (!string.IsNullOrEmpty(objEdit.Image))
                    System.IO.File.Delete(Path.Combine(HttpContext.Server.MapPath("~/Uploads"), objEdit.Image));
                uploadFile.SaveAs(filePath);
            }

            if (objEdit != null)
            {
                objEdit.ViName = category.ViName;
                objEdit.EnName = category.EnName;

                if (!string.IsNullOrEmpty(fileName))
                    objEdit.Image = fileName;

                repository.Update(objEdit);

                TempData[Constants.MESSAGE] = "Đã cập nhật thành công";
            }

            return RedirectToAction("Index");
        }

        public ActionResult Add()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Add(HttpPostedFileBase uploadFile)
        {
            string fileName = "";
            if (uploadFile != null && uploadFile.ContentLength > 0)
            {
                fileName = Path.GetFileName(uploadFile.FileName);
                string filePath = Path.Combine(HttpContext.Server.MapPath("~/Uploads"), fileName);
                uploadFile.SaveAs(filePath);
            }

            var category = new tkntCategory();

            TryUpdateModel(category);

            var repository = RepositoryManager.CreateTKNTCategoryRepository();

            category.Image = fileName;

            repository.Insert(category);

            TempData[Constants.MESSAGE] = "Đã thêm thành công";

            return RedirectToAction("Index");
        }

        //public ActionResult Search(string id)
        //{
        //    var repository = RepositoryManager.CreateTKNTCategoryRepository();

        //    var lst = repository.SearchByName(id);

        //    return View(lst);
        //}

        #endregion action

        #region private method

        private List<tkntCategory> Paging(int pageIndex, tkntCategoryRepository repository)
        {
            var totalPage = repository.GetTotalPage(Constants.NUM_OF_ROWS);

            ViewData[Constants.TOTAL_PAGE] = totalPage;

            ViewData[Constants.PAGE_INDEX] = pageIndex;

            if (pageIndex > totalPage)
                return null;

            var lst = repository.Paging(pageIndex, Constants.NUM_OF_ROWS);

            return lst;
        }

        #endregion private method
    }
}